package com.wxg.tcp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class talksend implements Runnable {
    DatagramSocket socket=null;
    BufferedReader reader=null;
    private String name=null;
    private int Ipfrom;
    private String toIp;
    private int toPort;

    public talksend(int ipfrom, String toIp, int toPort,String name) {
        this.name=name;
        this.Ipfrom = ipfrom;
        this.toIp = toIp;
        this.toPort = toPort;
        try {
            socket = new DatagramSocket(ipfrom);
            System.out.print(name+":");
            reader = new BufferedReader(new InputStreamReader(System.in));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void run() {



        while (true){
            try {
                String re=reader.readLine();
                byte[] bytes = re.getBytes();
                DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toIp,this.toPort));
                socket.send(packet);
                if (re.equals("bye")){
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }



        socket.close();

    }
}
